layui.define(['table', 'form', 'admin', 'upload'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    //
    table.render({
        elem: '#LAY-activity-manage'
        , url: '/admin/activity/getactivitys'
        , cols: [[
            {field: 'id', title: 'ID', width: 70}
            , {field: 'category', title: '类型', width: 150,  templet: function (d) {
                    var category = '';
                    switch (d.category) {
                        case 0:
                            category = '自定义';//未配置
                            break;
                        case 1:
                            category = '注册送';
                            break;
                        case 2:
                            category = '续费送';
                            break;
                    }

                    return category;
                }
            }
            , {field: 'title', title: '标题', width: 220}
            , {field: 'desc', title: '副标题', width: 190}
            , {
                field: '', title: 'PC封面', width: 100, templet: function (d) {
                    if (d.thumb_act) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.thumb_act + '"  value="' + d.thumb_act + '">';
                    }
                    return '无';
                }
            }
            , {
                field: '', title: 'PC详情封面', width: 120, templet: function (d) {
                    if (d.thumb_detail) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.thumb_detail + '"  value="' + d.thumb_detail + '">';
                    }
                    return '无';
                }
            }
            , {
                field: '', title: 'PC底部图片', width: 120, templet: function (d) {
                    if (d.thumb_url) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.thumb_url + '"  value="' + d.thumb_url + '">';
                    }
                    return '无';
                }
            }
            , {
                field: '', title: 'APP封面', width: 100, templet: function (d) {
                    if (d.pic_cover_app) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.pic_cover_app + '"  value="' + d.pic_cover_app + '">';
                    }
                    return '无';
                }
            }
            , {
                field: '', title: 'APP详情封面', width: 120, templet: function (d) {
                    if (d.pic_detail_app) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.pic_detail_app + '"  value="' + d.pic_detail_app + '">';
                    }
                    return '无';
                }
            }
            , {field: 'weight', title: '权重', width: 100}
            , {field: 'carat', title: '宝石', width: 100}
            , {field: 'experience', title: '经验值', width: 100}
            , {field: 'eagle_drill', title: '钻石', width: 100}
            /**
             , {
                field: '', title: '开启奖励', width: 100, templet: function (d) {
                    return d.open_reward == 0 ? '开启' : '未开启'
                }
            }
             , {
                field: '', title: '礼物', width: 150, templet: function (d) {
                    return d.gift_title ? d.gift_title + ' （ID' + d.gift_id + '）' : '';
                }
            }
             , {field: 'gift_num', title: '礼物数量', width: 100}
             , {field: 'props_name', title: '挂件信息', width: 200}
             **/
            , {field: 'start_time', title: '开始时间', width: 200}
            , {field: 'end_time', title: '结束时间', width: 200}
            , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#table-activitylist-activity'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    // 活动领取记录
    table.render({
        elem: '#LAY-activity-receive-manage'
        , url: '/admin/activity/receive_list'
        , cols: [[
            {field: 'id', title: 'ID', width: 70}
            , {field: 'category', title: '类型', width: 150,  templet: function (d) {
                    var category = '';
                    switch (d.category) {
                        case 0:
                            category = '未配置';
                            break;
                        case 1:
                            category = '注册送';
                            break;
                        case 2:
                            category = '续费送';
                            break;
                    }

                    return category;
                }
            }
            , {field: 'uid', title: '领取人ID', width: 120}
            , {
                field: '', title: '标题', minWidth: 220, templet: function (d) {
                    return d.activity ? d.activity.title : '';
                }
            }
            // , {field: 'gift_title', title: '奖励礼物', width: 100}
            // , {field: 'gift_num', title: '礼物数量', width: 90}
            // , {field: 'props_name', title: '挂件信息', width: 200}
            , {field: 'carat', title: '宝石', width: 100}
            , {field: 'eagle_drill', title: '钻石', width: 100}
            , {field: 'experience', title: '经验值', width: 100}
            , {
                field: '', title: '活动开始时间', width: 170, templet: function (d) {
                    return d.activity ? d.activity.start_time : '';
                }
            }
            , {
                field: '', title: '活动结束时间', width: 170, templet: function (d) {
                    return d.activity ? d.activity.end_time : '';
                }
            }
            , {field: 'create_time', title: '领取时间', width: 170}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });


    //查看照片
    admin.events.photosPreview = function (othis) {
        let urls = othis.attr("value");
        let urlarr = new Array();
        $.each(urls.split(","), function (i, val) {
            urlarr.push({"src": val});
        });
        layer.photos({
            photos: {
                "title": "查看照片" //相册标题
                , "data": urlarr
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    //监听工具条
    table.on('tool(LAY-activity-manage)', function (obj) {
        if (obj.event === 'del') {
            layer.confirm('确定要删除该条目吗？', function (index) {
                admin.req({
                    url: '/admin/activity/del_post'
                    , data: {"activityid": obj.data.id}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-activity-manage");
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            });
        } else if (obj.event === 'edit') {
            var layerForm = layer.open({
                type: 2
                , title: '编辑'
                , content: '/admin/activity/edit?id=' + obj.data.id
                , maxmin: true
                , area: ['650px', '650px']
            });
            layer.full(layerForm);
        }
    });

    //上传图片
    upload.render({
        url: '/admin/index/noOperation'
        , elem: '.LAY_iconImgUpload'
        , auto: true
        , choose: function (obj) {
            // 图片地址表单id
            var imginput_obj_id = this.idObjName;

            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: '/admin/activity/signForCos'
                , success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime
                                });
                            }
                        });
                        cos.putObject({
                            Bucket: res.data.bucket,
                            Region: res.data.region,
                            Key: 'images/' + res.data.filename + choosefile.name,
                            Body: choosefile,
                        }, function (err, data) {
                            layer.closeAll('loading');
                            if (err) {
                                console.log(err);
                                layer.msg("上传失败", {icon: 5});
                            } else {
                                if (data.statusCode == 200) {
                                    $('#' + imginput_obj_id).val("https://" + data.Location);
                                } else {
                                    layer.msg("图片上传失败", {icon: 5});
                                }
                            }
                        });
                    } else {
                        layer.closeAll('loading');
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            })
        }
    });

    //查看图标
    admin.events.coverImgPreview = function (othis) {
        var pic_obj_id = othis.data('idname');
        var src = $('#' + pic_obj_id).val();
        layer.photos({
            photos: {
                "title": "查看图标" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };


    exports('activity', {})
});